<?php

namespace Common\Models\Videos;

use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Validator\Uniqueness;

class VideoCategory extends Model
{
    protected $id;

    protected $name;

    protected $description;

    protected $parent_id;

    protected $id_path;

    protected $position;

    protected $status;

    public function getSource()
    {
        return 'video_category';
    }

    public function initialize()
    {
        $this->setSource('video_category');

        $this->hasMany('id' , 'Common\Models\Videos\VideoCategoryLink' , 'category_id' , array(
            'alias' => 'videoCategoryLink'
        ));
    }

    /**
     * @return User
     */
    public static function findFirstById($parameters = array())
    {
        return parent::findFirstById($parameters);
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param mixed $description
     */
    public function setDescription($description)
    {
        $this->description = $description;
    }

    /**
     * @return mixed
     */
    public function getDescription()
    {
        return $this->description;
    }



    /**
     * @param mixed $id_path
     */
    public function setIdPath($id_path)
    {
        $this->id_path = $id_path;
    }

    /**
     * @return mixed
     */
    public function getIdPath()
    {
        return $this->id_path;
    }

    /**
     * @param mixed $parent_id
     */
    public function setParentId($parent_id)
    {
        $this->parent_id = $parent_id;
    }

    /**
     * @return mixed
     */
    public function getParentId()
    {
        return $this->parent_id;
    }

    /**
     * @param mixed $position
     */
    public function setPosition($position)
    {
        $this->position = $position;
    }

    /**
     * @return mixed
     */
    public function getPosition()
    {
        return $this->position;
    }

    /**
     * @param mixed $status
     */
    public function setStatus($status)
    {
        $this->status = $status;
    }

    /**
     * @return mixed
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * Validate that emails are unique across users
     */
    public function validation()
    {
        /*
        $this->validate(new Uniqueness(array(
            "field" => "email",
            "message" => "The email is already registered"
        )));

        return $this->validationHasFailed() != true;*/
    }

    public static function fetchCategoryTree($parent , $spacing = '', $user_tree_array = '')
    {
        if (!is_array($user_tree_array))
            $user_tree_array = array();

        $categories = VideoCategory::find(array(
            "conditions" => "parent_id = ?1",
            "bind"       => array(1 => $parent)
        ));

        if ($categories) {
            foreach($categories as $category)
            {
                //$user_tree_array[] = array("id" => $category->getId() , "name" => $spacing . $category->getName());
                $user_tree_array[$category->getId()] =  $spacing . $category->getName();

                $user_tree_array = self::fetchCategoryTree($category->getId() , $spacing . '¦&nbsp;&nbsp;&nbsp;&nbsp;', $user_tree_array);
            }
        }
        return $user_tree_array;
    }

}
